//
//  ReadViewController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "ReadViewController.h"
#import "LeftImageCell.h"
#import "LeftOneRightTwoImageCell.h"
#import "NoImageCell.h"
#import "ReadListModel.h"
#import "ReadDetailController.h"

#define LEFTIndentifier @"Left"
#define ThreeImageIdentifier @"LeftOneRightTwo"   //左1右2
#define NoImageIdentifier @"noImage"  //没有图片
#define UpIdentifier @"BigImage"                 //上边一个大图

@interface ReadViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign) NSInteger count;
@property(nonatomic,retain)NSMutableArray *readArray;
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)BOOL isUpLoading;
@end

@implementation ReadViewController
-(void)dealloc{
    [_readArray release];
    [_myTableView release];
    [super dealloc];
}


-(void)viewWillAppear:(BOOL)animated{
     self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:212 / 255.0 green:25 / 255.0 blue:37 / 255.0 alpha:1];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    self.readArray  = [NSMutableArray array];
 
    
    [self initUI];
    [self registerCell];
    [self addHeader];
    [self addFooter];
    
    
    
    
    
    
}






-(void)addHeader
{
    
    self.myTableView.headerHidden = NO;
    __block ReadViewController *vc = self;
    
    [self.myTableView addHeaderWithCallback:^{
        
        vc.count = 6;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.count];
        
    }];
   [self.myTableView headerBeginRefreshing];
}

-(void)addFooter
{
    __block ReadViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        vc.count += 6;
        vc.isUpLoading  = YES;
        
        [vc GetPersonList:vc.count];
    }];
    
    
}


-(void)GetPersonList:(NSInteger)nextCursor
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    NSString *getstr = [NSString stringWithFormat:@"http://c.3g.163.com/recommend/getSubDocPic?passport=b949a6faa3469bad7e078629c196a3c3@tencent.163.com&devId=9479B6CD-C578-4931-A034-AA16A23E62AC&size=%ld&from=yuedu",self.count];
    
    [mgr GET:getstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.readArray removeAllObjects];
        }
        NSMutableArray *allArr= [responseObject objectForKey:@"推荐"];
        
        
        for (NSDictionary *dic in allArr) {
          
            
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            if (model.imgnewextra) {
                model.flag = 3;// 标记三个图的cell
            } else if (model.imgsrc) {
                model.flag = 1; // 标记一个图的cell
            } else {
                model.flag = 0;  //标记没有图片的cell
            }
            [self.readArray addObject:model];
            
            
        }
      
     
        
        //        self.page += 10;
        // 刷新表格
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mb.mode = MBProgressHUDModeText;
        mb.labelText = @"网络不给力呀";
        [mb hide:YES afterDelay:0.5];
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.readArray.count;
}



-(void)initUI
{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(10 *SCREEN_WIDTH, 0, 355 * SCREEN_WIDTH, 657 * SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
}


-(void)registerCell{
    
    [self.myTableView registerClass:[LeftImageCell class] forCellReuseIdentifier:LEFTIndentifier];
    
    [self.myTableView registerClass:[LeftOneRightTwoImageCell class] forCellReuseIdentifier:ThreeImageIdentifier];
    
    [self.myTableView registerClass:[NoImageCell class] forCellReuseIdentifier:NoImageIdentifier];
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadListModel *model = self.readArray[indexPath.row];
    //左一图
    if (model.flag == 1) {
        LeftImageCell *cell = [tableView dequeueReusableCellWithIdentifier:LEFTIndentifier];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        //纯文字
    }else if (model.flag == 0){
        NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NoImageIdentifier];
        
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        LeftOneRightTwoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:ThreeImageIdentifier];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
   
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadListModel *model = self.readArray[indexPath.row];
    
    ReadDetailController *vc = [[ReadDetailController alloc]init];
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadListModel *model = self.readArray[indexPath.row];
    if (model.flag == 1) {
        return [LeftImageCell heightForRowWithReadNews:model];
    }else if (model.flag == 0) {
        return  80;
    }else{
        return [LeftOneRightTwoImageCell heightForRowWithReadNews:model];
    }
}

























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
